/*
 * Anemometer.c
 *
 * Created: 17/02/2012 14:22:49
 *  Author: Dani
 * 
 * External clock : PB0
 */ 

#include <avr/io.h>
#define F_CPU 3686400
//#include <util/delay.h>
#include <avr/interrupt.h>
#include "lcd162.h"
#include "led.h"

volatile int counter;
volatile int flag=0;


ISR (TIMER1_OVF_vect)
{
	counter = TCNT0;
	flag=1;
	//Reload counter in Timer 0
	TCNT0 = 0;
   
	// Reload Timer1 to 1 second to next overflow
	TCNT1 = 65536-3600;

}

void InitTimer0()
{
	
	//External clock falling edge.
	//Normal Mode
	TCCR0 = 0b00000110;
	TCNT0=0;
}

void InitTimer1()
{
  // Normal mode, no waveform generation
  TCCR1A = 0b00000000;
  // Normal mode, clock prescaler PS = 1024
  TCCR1B = 0b00000101;
  // 1 second to next overflow:
  // 3686400/1024/3600 = 1
  TCNT1 = 65536-3600;
  // Enable Timer1 overflow interrupt
  TIMSK |= 0b00000100;
}

int main(void)
{

	
	cli();
	LCDInit();
	InitTimer0();
	InitTimer1();
	sei();
	
	counter = 0;	
    while(1)
    {
		if(flag==0)
			continue;
			
		cli();
		int c=counter;
		flag=0;
		sei();
		
		LCDGotoXY(0,0);
		LCDDispInteger(c);
		LCDDispString("  Hz  ");
    }
}